package queue;

/**
 * @author tongchen
 * @create 2022-10-18 9:32
 */
public class MYQueue_SingleList {
    public static void main(String[] args) {
        MYQueue_SingleList myQueue_singleList = new MYQueue_SingleList();
        myQueue_singleList.offer(12);
        myQueue_singleList.offer(13);
        myQueue_singleList.offer(14);
        myQueue_singleList.offer(15);
        myQueue_singleList.offer(16);
        myQueue_singleList.poll();
        myQueue_singleList.poll();
        myQueue_singleList.poll();
        myQueue_singleList.poll();
    }
    //用单链表实现队列
    ListNode head;
    ListNode tail;
    public void addLast(int value){
        //创建新结点
        ListNode listNode = new ListNode(value);
        if (head==null){
            head=listNode;
            tail=listNode;

        }
        else{
            ListNode cur=head;
            while(cur.next!=null){
                cur=cur.next;
            }
            cur.next=listNode;
            tail=listNode;
        }
    }
    public void display(){
        ListNode cur=head;
        while (cur!=null){
            System.out.println(cur);
            cur=cur.next;
        }
    }
    //单链表功能基本实现，现在用单链表实现队列(队列规则，尾进头出)
    public void offer(int val){
        ListNode listNode = new ListNode(val);
        if(tail==null){
            head=tail=listNode;
        }

        tail.next=listNode;
        tail=tail.next;
    }
    public void poll(){
        if (head==null){
            throw new RuntimeException("队列为空，无法拿出数据");
        }
        else {
            head=head.next;
        }
    }



}
class ListNode{
    int value;
    ListNode next;

    public ListNode(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "value=" + value +
                '}';
    }
}
